package com.software.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.software.bean.ProductBean;
import com.software.bean.ProductImageBean;
import com.software.dto.ProductDetailImageDto;
import com.software.mapper.ProductImageMapper;
import com.software.mapper.ProductMapper;
import com.software.service.ProductImageService;
import com.software.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wyj
 * @since 2024-03-24
 */
@Service
public class ProductImageServiceImpl extends ServiceImpl<ProductImageMapper, ProductImageBean> implements ProductImageService {

    @Autowired
    private ProductImageMapper imageMapper;

    @Autowired
    private ProductMapper productMapper;

    /**
     * @description:循环多图上传，上传发布的详情照片
     * @author: wyj
     * @date: 2024/3/29 11:52
     * @param:pid，照片
     * @return:受影响的行数
     */
    @Override
    public Integer postDetailImage(ProductDetailImageDto detailImageDto) {
        //参数校验
        AssertUtil.isTrue(detailImageDto.getFile() == null, "参数异常");
        AssertUtil.isTrue(detailImageDto.getPid() == null || detailImageDto.getPid() == 0, "参数异常");
        //校验pid是否合法
        ProductBean productBean = productMapper.selectById(detailImageDto.getPid());
        AssertUtil.isTrue(productBean == null, "返回值异常喽");

        ProductImageBean productImageBean = new ProductImageBean();
        productImageBean.setStatus(1);
        productImageBean.setProductId(detailImageDto.getPid());
        productImageBean.setProductDetailImage(saveImage(detailImageDto.getFile()));

        int insert = imageMapper.insert(productImageBean);
        return insert == 1 ? 1 : 0;
    }

    /**
     * @description:后台审核详情图片
     * @author: wyj
     * @date: 2024/4/2 14:11
     * @param:
     * @return:该发布下的图片列表
     */
    @Override
    public List<ProductImageBean> getProductDetailImageList(Integer productId) {
        AssertUtil.isTrue(productId == null || productId == 0, "参数异常");
        List<ProductImageBean> list = imageMapper.getProductDetailImageListByProductId(productId);
        AssertUtil.isTrue(list.size() == 0, "返回值异常");
        return list;
    }

    private String saveImage(MultipartFile file) {
        String realPath = "E:/img";
        String fileName = UUID.randomUUID().toString().replace("-", "");
        String imgURL = "http://localhost:8082/file/" + fileName +
                file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String filePath = realPath + "/" + fileName + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        try {
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return imgURL;
    }

}
